#pragma once

#include <functional>
#include <netinet/in.h>
#include <memory>

class EventLoop;
class Channel;

class Acceptor {
public:
    using NewConnectionCallback = std::function<void(int sockfd, const sockaddr_in&)>;

    Acceptor(EventLoop* loop, const sockaddr_in& listenAddr);
    ~Acceptor();

    void listen();

    void setNewConnectionCallback(const NewConnectionCallback& cb) { newConnectionCallback_ = cb; }

private:
    void handleRead();

    int createNonblockingSocket();

    EventLoop* loop_;
    int listenFd_;
    sockaddr_in listenAddr_;
    std::unique_ptr<Channel> acceptChannel_;
    NewConnectionCallback newConnectionCallback_;
};
